package com.conversationboard.model;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.conversationboard.formatter.Formatter;
import com.conversationboard.formatter.IllegalMessageException;
import com.conversationboard.model.enums.MessageFormat;
import com.conversationboard.site.Site;

/**
 * A question represents an ingoing thread, i.e. a new thread on the database.
 * There could be an argument for it being the same as an outgoing thread, i.e.
 * the Thread class; however some processing subtley differs, e.g. escaping of
 * HTML.
 * 
 * @author Keith Watson
 * 
 */

public class Question {

    private String title;
    private String text;
    private int boardId;
    private User user;
    private boolean hidden = false;
    private MessageFormat messageFormat = MessageFormat.ESCAPE_HTML;
    private Date timestamp = new Date();
    private List<PollItem> pollItems = null;
    private Date pollClosingDate = null;


    public Date getPollClosingDate() {
        return pollClosingDate;
    }


    public void setPollClosingDate(Date pollClosingDate) {
        this.pollClosingDate = pollClosingDate;
    }


    public boolean isPoll() {
        return pollItems != null;
    }


    public List<PollItem> getPollItems() {
        return pollItems;
    }


    public void setPollItems(List<PollItem> pollItems) {
        this.pollItems = pollItems;
    }


    public Question(String title, String text, int boardId, User user) throws SQLException {
        this.title = title;
        this.text = text;
        this.user = user;
        this.boardId = boardId;
    }


    public Timestamp getTimestamp() {
        return new Timestamp(timestamp.getTime());
    }


    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }


    public boolean isHidden() {
        return hidden;
    }


    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }


    public String getBody() throws IllegalMessageException, SQLException {

        String text = null;

        Substitutions substitutions = Site.getSubstitutions();
        text = substitutions.substitute(this.user, this.text);
        
        if (this.messageFormat == MessageFormat.ESCAPE_HTML) {
            text = Formatter.escapeHtml(text);
        } else if (this.messageFormat == MessageFormat.RAW) {
        } else {
            throw new UnsupportedOperationException("messageFormat should be either ESCAPE_HTML or RAW");
        }

        /* Perform the BBCode substitutions */
        Board board = Boards.getBoard(this.boardId);
        text = Formatter.asHtml(board, this.user, text, false);
        
        /* Munge email addresses */
        text = Formatter.mungeEmailAddr(this.user, text);

        return text;

    }


    public void setText(String text) {
        this.text = text;
    }


    /**
     * If we are escaping HTML, then return the escaped version
     * 
     * @return the title, with any embedded HTML escaped
     */

    public String getTitle() {

        String title = null;

        Substitutions substitutions = Site.getSubstitutions();
        title = substitutions.substitute(this.user, this.title);

        if (this.messageFormat == MessageFormat.ESCAPE_HTML) {
            title = Formatter.escapeHtml(title);
        } else if (this.messageFormat == MessageFormat.RAW) {
        } else {
            throw new UnsupportedOperationException("messageFormat should be either ESCAPE_HTML or RAW");
        }

        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }


    public MessageFormat getMessageFormat() {
        return messageFormat;
    }


    public void setMessageFormat(MessageFormat messageFormat) {
        this.messageFormat = messageFormat;
    }

}
